package com.sw.bbs.board.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sw.bbs.board.dao.BoardMapper;
import com.sw.bbs.board.dao.CommentMapper;
import com.sw.bbs.board.dao.PostMapper;
import com.sw.bbs.board.dao.user.*;
import com.sw.bbs.board.pojo.Comment;
import com.sw.bbs.board.pojo.user.UserBoardOperation;
import com.sw.bbs.board.pojo.user.UserPostOperation;
import com.sw.bbs.board.vo.request.OperaCommonRequest;
import com.sw.bbs.board.vo.request.board.OperaBoardRequest;
import com.sw.bbs.board.vo.request.comment.OperaCommentRequest;
import com.sw.bbs.board.vo.request.post.OperaPostRequest;
import com.sw.bbs.board.vo.response.board.BoardOperaResponse;
import com.sw.bbs.board.vo.response.post.PostOperaResponse;
import com.sw.bbs.common.common.ErrorCode;
import com.sw.bbs.common.common.ServiceException;
import com.sw.bbs.board.pojo.Board;
import com.sw.bbs.board.pojo.Post;
import com.sw.bbs.board.service.ProfileService;
import com.sw.bbs.board.vo.request.QueryCommonRequest;
import com.sw.bbs.board.vo.request.QueryWithUserRequest;
import com.sw.bbs.user.pojo.User;
import com.sw.bbs.user.service.UserService;
import javafx.geometry.Pos;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProfileServiceImpl implements ProfileService {



    @Autowired
    private UserService userService;

    @Autowired
    private BoardMapper boardMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 用户创建的板块列表
     *
     * @param queryCommonRequest
     * @param httpServletRequest
     * @return
     */
    @Override
    public PageInfo boardList(QueryCommonRequest queryCommonRequest, HttpServletRequest httpServletRequest) throws Exception {

        User user = userService.getUserByRequest(httpServletRequest);
        try {
            List<Board> boardList = boardMapper.queryBoardsByUserId(user.getUserId());
            PageInfo<Board> appsPageInfo = new PageInfo<>(boardList);
            return appsPageInfo;

        }catch (Exception e) {
            log.error("boardList error");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"boardList error");
        }
    }


    /**
     * 用户收藏的板块列表
     *
     * @param queryCommonRequest
     * @param httpServletRequest
     * @return
     */
    @Override
    public PageInfo boardCollectList(QueryCommonRequest queryCommonRequest, HttpServletRequest httpServletRequest) {
        User user = userService.getUserByRequest(httpServletRequest);
        try {
            List<BoardOperaResponse> boardList = boardMapper.queryProfileBoardsByType(user.getUserId(),3);
            PageInfo<BoardOperaResponse> appsPageInfo = new PageInfo<>(boardList);
            return appsPageInfo;
        }catch (Exception e) {
            log.error("boardCollectList error");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"boardCollectList error");
        }
    }

    /**
     * 用户创建的帖子列表
     *
     * @param queryCommonRequest
     * @param httpServletRequest
     * @return
     */
    @Override
    public PageInfo<?> postList(QueryCommonRequest queryCommonRequest, HttpServletRequest httpServletRequest) {

        User user = userService.getUserByRequest(httpServletRequest);
        try {
            List<Post> posts = postMapper.queryPostsByUserId(user.getUserId());
            PageInfo<Post> appsPageInfo = new PageInfo<>(posts);
            return appsPageInfo;

        }catch (Exception e) {
            log.error("boardList error");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"boardList error");
        }
    }

    /**
     * 用户收藏的帖子列表
     *
     * @param queryCommonRequest
     * @param httpServletRequest
     * @return
     */
    @Override
    public PageInfo postCollectList(QueryCommonRequest queryCommonRequest, HttpServletRequest httpServletRequest) {
        User user = userService.getUserByRequest(httpServletRequest);
        try {
            List<PostOperaResponse> operaResponses = postMapper.queryPostByType(user.getUserId(),3);
            PageInfo<PostOperaResponse> appsPageInfo = new PageInfo<>(operaResponses);
            return appsPageInfo;

        }catch (Exception e) {
            log.error("boardList error");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"boardList error");
        }
    }

    /**
     * 用户关注的帖子列表
     *
     * @param queryCommonRequest
     * @param httpServletRequest
     * @return
     */
    @Override
    public PageInfo postFollowList(QueryCommonRequest queryCommonRequest, HttpServletRequest httpServletRequest) {
        User user = userService.getUserByRequest(httpServletRequest);
        try {
            List<PostOperaResponse> operaResponses = postMapper.queryPostByType(user.getUserId(),4);
            PageInfo<PostOperaResponse> appsPageInfo = new PageInfo<>(operaResponses);
            return appsPageInfo;

        }catch (Exception e) {
            log.error("boardList error");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"boardList error");
        }
    }

    /**
     * 用户创建的评论列表
     *
     * @param queryCommonRequest
     * @param httpServletRequest
     * @return
     */
    @Override
    public PageInfo commentList(QueryCommonRequest queryCommonRequest, HttpServletRequest httpServletRequest) {
        User user = userService.getUserByRequest(httpServletRequest);
        try {
            List<Comment> comments = commentMapper.queryCommentsByUserId(user.getUserId());
            PageInfo<Comment> appsPageInfo = new PageInfo<>(comments);
            return appsPageInfo;

        }catch (Exception e) {
            log.error("commentList error");
            throw new ServiceException(ErrorCode.BUSINESS_SERVER_ERROR,"commentList error");
        }
    }



}
